package cn.circle.system.util;

import java.util.ArrayList;
import java.util.List;

import cn.circle.system.model.SysMenus;
import cn.circle.system.pagemodel.SimpleTreeNode;

/**
 * 功能说明：把通过SQL语句获取的数据，通过遍历进行填充
 */
public class SimpleMenuTreeUtil {
	private List<SysMenus> lstAll;

	public List<SysMenus> getLstAll() {
		return lstAll;
	}

	public void setLstAll(List<SysMenus> lstAll) {
		this.lstAll = lstAll;
	}
	
    public List<SimpleTreeNode> findById(Integer parentId)
    {
        List<SimpleTreeNode> subs = new ArrayList<SimpleTreeNode>();
        for(int i=0;i<lstAll.size();i++)
        {
        	SimpleTreeNode mod = recordToTreeNode((SysMenus)lstAll.get(i));
            if(parentId==null)
            {
                if(mod.getPid()==null || mod.getPid() == 0)
                {
                    lstAll.remove(i);
                    i--;
                    List<SimpleTreeNode> modSubs = findById(mod.getId());
                    if(modSubs.size()>0)
                    {
                        mod.setChildren(modSubs);
                    }
                    subs.add(mod);
                }
            }
            else
            {
                if(mod.getPid() != null && mod.getPid().longValue() == parentId.longValue())
                {
                    List<SimpleTreeNode> modSubs = findById(mod.getId());
                    if(modSubs.size()>0)
                    {
                    	mod.setChildren(modSubs);
                    }
                    subs.add(mod);
                }
            }
        }
        return subs;
    }

    private SimpleTreeNode recordToTreeNode(SysMenus sysMenus)
    {
    	SimpleTreeNode td = new SimpleTreeNode();
    	td.setId(sysMenus.getId());
    	td.setText(sysMenus.getName());
    	td.setPid(sysMenus.getParentId());
    	return td;
    }

}
